home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / ici / ici.cpi / unary.c < prev    next >
C/C++ Source or Header  |  1994-10-27  |  1KB  |  69 lines

  1. #include "exec.h"
  2. #include "float.h"
  3. #include "int.h"
  4. #include "op.h"
  5. #include "parse.h"
  6. #include "buf.h"
  7. #include "null.h"
  8.  
  9. int
  10. op_unary()
  11. {
  12.     int_t    *i;
  13.     float_t    *f;
  14.  
  15.     switch (opof(x_top[-1])->op_code)
  16.     {
  17.     case t_subtype(T_EXCLAM):
  18.     if (isfalse(o_top[-1]))
  19.         o_top[-1] = objof(o_one);
  20.     else
  21.         o_top[-1] = objof(o_zero);
  22.     --x_top;
  23.     return 0;
  24.  
  25.     case t_subtype(T_TILDE):
  26.     if (!isint(o_top[-1]))
  27.         goto fail;
  28.     if ((i = new_int(~intof(o_top[-1])->i_value)) == NULL)
  29.         return 1;
  30.     o_top[-1] = objof(i);
  31.     loose(i);
  32.     --x_top;
  33.     return 0;
  34.  
  35.     case t_subtype(T_MINUS):
  36.     if (isint(o_top[-1]))
  37.     {
  38.         if ((i = new_int(-intof(o_top[-1])->i_value)) == NULL)
  39.         return 1;
  40.         o_top[-1] = objof(i);
  41.         loose(i);
  42.         --x_top;
  43.         return 0;
  44.     }
  45.     else if (isfloat(o_top[-1]))
  46.     {
  47.         if ((f = new_float(-floatof(o_top[-1])->f_value)) == NULL)
  48.         return 1;
  49.         o_top[-1] = objof(f);
  50.         loose(f);
  51.         --x_top;
  52.         return 0;
  53.     }
  54.     fail:
  55.     default:
  56.     switch (opof(x_top[-1])->op_code)
  57.     {
  58.     case t_subtype(T_EXCLAM): error = "!"; break;
  59.     case t_subtype(T_TILDE): error = "~"; break;
  60.     case t_subtype(T_MINUS): error = "-"; break;
  61.     default: error = "<unknown unary operator>"; break;
  62.     }
  63.     sprintf(buf, "attempt to perform \"%s %s\"",
  64.         error, o_top[-1]->o_type->t_name);
  65.     error = buf;
  66.     return 1;
  67.     }
  68. }
  69.